<?php

require_once 'FormValidator.php';

abstract class SingleValidationHandler{
    
    const STATUS_NOT_HANDLED = 0;
    const STATUS_SUCCESS = 1;
    const STATUS_ERROR = 2;
    const STATUS_HANDLED = 3;
    
    private $validator;
    private $triggerParam;
    private $status = self::STATUS_NOT_HANDLED;

    public function getStatus(){return $this->status;}
    protected function setStatus($status){$this->status = $status;}

    public function __construct(FormValidator $validator, $triggerParam){
        $this->validator = $validator;
        $this->triggerParam = $triggerParam;
    }

    public function tryProcess(){
        $trigger = $this->validator->getOwner()->getRequest()
             ->getParameter($this->triggerParam, $this->validator->getMethod());

        if($trigger !== null){
            if($this->status != self::STATUS_NOT_HANDLED)return $this->status;
            if($this->processOutput($this->validator->validate()))
                $this->status = self::STATUS_SUCCESS;
            else $this->status = self::STATUS_ERROR;
            return $this->status;
        }

        if($this->status === self::STATUS_SUCCESS)
            $this->status = self::STATUS_HANDLED;
        else if($this->status === self::STATUS_ERROR)
            $this->status = self::STATUS_NOT_HANDLED;
        return $this->status;
    }

    protected abstract function processOutput($validationOutput);

}
?>
